package cn.cloud.butler.webapi.service;

import cn.cloud.butler.webapi.domain.HealhSecurityEntity;
import cn.cloud.butler.webapi.repository.HealhSecurityRepository;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class MachineHealthService {

    private  final HealhSecurityRepository healhSecurityRepository;

    @Autowired
    public MachineHealthService(HealhSecurityRepository healhSecurityRepository) {
        this.healhSecurityRepository = healhSecurityRepository;
    }

    //接口单台机床健康状态趋势图<br></a>
    //http://10.10.56.52:8080/HncWeb/healTendency.action?macsn=20CF1B9BDFF3733<br>
    @Cacheable(value = "healTendency-key")
    public List<HealhSecurityEntity> healTendency(String macsn) {
        return  healhSecurityRepository.findByMacSN(macsn);

    }

    //接口53
    @Cacheable(value = "LastHealthSecurity-key") //此时缓存的是List
    public List<HealhSecurityEntity> LastHealthSecurity(String macsn) {
        List<HashMap<String,Object>>  alarmDataes = new ArrayList<>(); //返回结果
        //是选择性查询，需要接口来接收结果
        return healhSecurityRepository.LastHealthSecurity(macsn);
    }

    /**
     * 查询多台机床所有的健康状态值，通过传递不同的type确定y轴不同
     * @param type 轴类型
     * @param macsns 机床SN列表
     * @return List<HashMap<String,Object>>
     */
    public List<HashMap<String,Object>> moreMacHealthByType(String type,String macsns){
        List<HashMap<String,Object>> data = new ArrayList<>();
        String[] macsnArray = macsns.split(",");
        List<String> macsnList = new ArrayList<>();
        for(int i = 0; i<macsnArray.length;i++){
            macsnList.add(macsnArray[i]);
        }

        List<HealhSecurityEntity> healSecurityEntities = healhSecurityRepository.findByMacsnIn(macsnList);

        HashMap<String,Object> map = new HashMap<>();

        for(HealhSecurityEntity healhSecurityEntity:healSecurityEntities){
            if(type.equals("mainAxis")){
                map.put("macsn", healhSecurityEntity.getMacsn());
                map.put("value", healhSecurityEntity.getMainAxis());
                map.put("uploadTime", healhSecurityEntity.getUploadTime());
                data.add(map);
            }else if(type.equals("xAxis")){
                map.put("macsn", healhSecurityEntity.getMacsn());
                map.put("value", healhSecurityEntity.getxAxis());
                map.put("uploadTime", healhSecurityEntity.getUploadTime());
                data.add(map);
            }else if(type.equals("yAxis")){
                map.put("macsn", healhSecurityEntity.getMacsn());
                map.put("value", healhSecurityEntity.getyAxis());
                map.put("uploadTime", healhSecurityEntity.getUploadTime());
                data.add(map);
            }else if(type.equals("zAxis")){
                map.put("macsn", healhSecurityEntity.getMacsn());
                map.put("value", healhSecurityEntity.getzAxis());
                map.put("uploadTime", healhSecurityEntity.getUploadTime());
                data.add(map);
            }else if(type.equals("toolMagazine")){
                map.put("macsn", healhSecurityEntity.getMacsn());
                map.put("value", healhSecurityEntity.getToolMagazine());
                map.put("uploadTime", healhSecurityEntity.getUploadTime());
                data.add(map);
            }else{

            }
        }
        return data;

    }
}
